package com.th.supcom.pds.deptdispensary.client.widget;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.th.supcom.framework.web.gwt.client.widget.FcMessageBox;
import com.th.supcom.pds.common.client.model.CodeConstants;
import com.th.supcom.pds.common.constant.KeysConstants;

/**
 * 药房护理单元配置事件中心
 * 
 * @author 杨冲
 */
public class DeptVsDispensaryListener
{
    private DeptVsDispensaryButtons m_buttons;
    private DeptVsDispensaryFields m_fields;
    private DeptVsDispensaryAction m_action;

    private DeptVsDispensaryEditListener m_editListener;

    /**
     * 构造函数
     */
    public DeptVsDispensaryListener ()
    {

    }

    /**
     * 设置按钮组件
     * 
     * @param buttons
     */
    public void setButtons (DeptVsDispensaryButtons buttons)
    {

        this.m_buttons = buttons;
    }

    /**
     * 设置文本组件
     * 
     * @param fields
     */
    public void setFields (DeptVsDispensaryFields fields)
    {

        this.m_fields = fields;
    }

    /**
     * 设置Action
     * 
     * @param action
     */
    public void setAction (DeptVsDispensaryAction action)
    {

        m_action = action;
    }

    /**
     * 初始化事件
     */
    public void initListeners ()
    {
        m_editListener = new DeptVsDispensaryEditListener ();
        m_editListener.setAction (m_action);
        m_editListener.setDeptVsDispensaryButtons (m_buttons);
        m_editListener.setDeptVsDispensaryFields (m_fields);
        m_editListener.initListeners ();

        addAddListener ();
        addEditListener ();
        addRefreshListener ();
        addDeleteListener ();
    }

    /**
     * 新增按钮事件
     */
    private void addAddListener ()
    {
        m_buttons.getAddButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                showWindow (false);
            }
        });
    }

    /**
     * 编辑按钮事件监听
     */
    private void addEditListener ()
    {
        m_buttons.getEditButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {

                edit ();
            }

        });
    }

    /**
     * 编辑
     */
    private void edit ()
    {
        if (!m_action.getGridSelectedStatus ())
        {
            MessageBox.alert (CodeConstants.PROMPT, "没有选中编辑内容", null);
            return;
        }

        showWindow (true);
    }

    /**
     * 新增删除按钮事件监听
     */
    private void addDeleteListener ()
    {

        m_buttons.getDeleteButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                delete ();
            }

        });
    }

    /**
     * 删除
     */
    private void delete ()
    {
        if (!m_action.getGridSelectedStatus ())
        {
            MessageBox.alert (CodeConstants.PROMPT, "没有选中删除内容", null);
            return;
        }

        FcMessageBox.confirm (CodeConstants.PROMPT, "确认删除？", new Listener <MessageBoxEvent> ()
        {

            @Override
            public void handleEvent (MessageBoxEvent be)
            {

                if (be.getButtonClicked ().getText ().equals ("否"))
                {
                    return;
                }

                m_action.delete ();
            }
        });
    }

    /**
     * 刷新事件监听
     */
    private void addRefreshListener ()
    {
        m_buttons.getRefreshButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_action.refresh ();
            }

        });
    }

    /**
     * 弹出新增/编辑窗口，true为编辑,false为新增
     * 
     * @param isEdit
     */
    private void showWindow (boolean isEdit)
    {
        DeptVsDispensaryEditWindow window = new DeptVsDispensaryEditWindow (isEdit);
        window.setFields (m_fields);
        window.setButtons (m_buttons);
        window.initUI ();

        m_action.setWindow (window);

        if (isEdit)
        {
            m_action.initDataWhenEdit ();
        }
        else
        {
            m_action.clearEditWindow ();
        }

        m_editListener.setWindow (window);
        m_editListener.setEdit (isEdit);
        window.show ();
    }

    /**
     * 添加快捷键事件
     */
    public void addEnterKeyListener (Component component)
    {
        new KeyNav <ComponentEvent> (component)
        {
            @Override
            public void onKeyPress (ComponentEvent ce)
            {
                int keyCode = ce.getKeyCode ();

                if (!ce.isAltKey ())
                {
                    return;
                }

                executeAltKey (keyCode);
            }
        };
    }

    /**
     * 执行Alt键事件
     */
    private void executeAltKey (int keyCode)
    {
        if (KeysConstants.VK_A == keyCode)
        {
            showWindow (false);
        }

        if (KeysConstants.VK_X == keyCode)
        {
            edit ();
        }

        if (KeysConstants.VK_D == keyCode)
        {
            delete ();
        }

        if (KeysConstants.VK_F == keyCode)
        {
            m_action.refresh ();
        }
    }
}
